package DevCode.Servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DevCode.Excepcion.DAOExcepcion;
import DevCode.Modelo.Usuario;
import DevCode.Negocio.GestionUsuario;

/**
 * Servlet implementation class BuscarUsuarioServlet
 */
@WebServlet("/BuscarUsuarioServlet")
public class BuscarUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscarUsuarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String USvnombres = request.getParameter("txtnombresb");
		int	CFicodigo = Integer.parseInt(request.getParameter("cboinstitucionb"));			
		int busqueda;
			if(CFicodigo == 0){
				busqueda=1;
			}else{
				busqueda=0;
			}
		GestionUsuario negocio = new GestionUsuario();
		try {
			Collection<Usuario> lista = negocio.buscarUsuario(USvnombres, CFicodigo, busqueda);
			request.setAttribute("USvnombresb", USvnombres);
			request.setAttribute("CFicodigob", CFicodigo);
			request.setAttribute("BUSQUEDA_USUARIO", lista);
			RequestDispatcher rd = request.getRequestDispatcher("Mantener_Usuarios.jsp");
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Error.jsp");
			rd.forward(request, response);
		}
	}

}
